home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap11 / About3 / About3.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.9 KB  |  157 lines

  1. /*------------------------------------------
  2.    ABOUT3.C -- About Box Demo Program No. 3
  3.                (c) Charles Petzold, 1998
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10. BOOL    CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
  11. LRESULT CALLBACK EllipPushWndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15. {
  16.      static TCHAR szAppName[] = TEXT ("About3") ;
  17.      MSG          msg ;
  18.      HWND         hwnd ;
  19.      WNDCLASS     wndclass ;
  20.  
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = szAppName ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.      
  39.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  40.      wndclass.lpfnWndProc   = EllipPushWndProc ;
  41.      wndclass.cbClsExtra    = 0 ;
  42.      wndclass.cbWndExtra    = 0 ;
  43.      wndclass.hInstance     = hInstance ;
  44.      wndclass.hIcon         = NULL ;
  45.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  46.      wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ;
  47.      wndclass.lpszMenuName  = NULL ;
  48.      wndclass.lpszClassName = TEXT ("EllipPush") ;
  49.  
  50.      RegisterClass (&wndclass) ;
  51.      
  52.      hwnd = CreateWindow (szAppName, TEXT ("About Box Demo Program"),
  53.                           WS_OVERLAPPEDWINDOW,
  54.                           CW_USEDEFAULT, CW_USEDEFAULT,
  55.                           CW_USEDEFAULT, CW_USEDEFAULT,
  56.                           NULL, NULL, hInstance, NULL) ;
  57.      
  58.      ShowWindow (hwnd, iCmdShow) ;
  59.      UpdateWindow (hwnd) ; 
  60.      
  61.      while (GetMessage (&msg, NULL, 0, 0))
  62.      {
  63.           TranslateMessage (&msg) ;
  64.           DispatchMessage (&msg) ;
  65.      }
  66.      return msg.wParam ;
  67. }
  68.  
  69. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  70. {
  71.      static HINSTANCE hInstance ;
  72.      
  73.      switch (message)
  74.      {
  75.      case WM_CREATE :
  76.           hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  77.           return 0 ;
  78.           
  79.      case WM_COMMAND :
  80.           switch (LOWORD (wParam))
  81.           {
  82.           case IDM_APP_ABOUT :
  83.                DialogBox (hInstance, TEXT ("AboutBox"), hwnd, AboutDlgProc) ;
  84.                return 0 ;
  85.           }
  86.           break ;
  87.           
  88.      case WM_DESTROY :
  89.           PostQuitMessage (0) ;
  90.           return 0 ;
  91.      }
  92.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  93. }
  94.  
  95. BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, 
  96.                             WPARAM wParam, LPARAM lParam)
  97. {
  98.      switch (message)
  99.      {
  100.      case WM_INITDIALOG :
  101.           return TRUE ;
  102.           
  103.      case WM_COMMAND :
  104.           switch (LOWORD (wParam))
  105.           {
  106.           case IDOK :
  107.                EndDialog (hDlg, 0) ;
  108.                return TRUE ;
  109.           }
  110.           break ;
  111.      }
  112.      return FALSE ;
  113. }
  114.  
  115. LRESULT CALLBACK EllipPushWndProc (HWND hwnd, UINT message, 
  116.                                    WPARAM wParam, LPARAM lParam)
  117. {
  118.      TCHAR       szText[40] ;
  119.      HBRUSH      hBrush ;
  120.      HDC         hdc ;
  121.      PAINTSTRUCT ps ;
  122.      RECT        rect ;
  123.      
  124.      switch (message)
  125.      {
  126.      case WM_PAINT :
  127.           GetClientRect (hwnd, &rect) ;
  128.           GetWindowText (hwnd, szText, sizeof (szText)) ;
  129.           
  130.           hdc = BeginPaint (hwnd, &ps) ;
  131.           
  132.           hBrush = CreateSolidBrush (GetSysColor (COLOR_WINDOW)) ;
  133.           hBrush = (HBRUSH) SelectObject (hdc, hBrush) ;
  134.           SetBkColor (hdc, GetSysColor (COLOR_WINDOW)) ;
  135.           SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT)) ;
  136.           
  137.           Ellipse (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  138.           DrawText (hdc, szText, -1, &rect,
  139.                     DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
  140.           
  141.           DeleteObject (SelectObject (hdc, hBrush)) ;
  142.           
  143.           EndPaint (hwnd, &ps) ;
  144.           return 0 ;
  145.           
  146.      case WM_KEYUP :
  147.           if (wParam != VK_SPACE)
  148.                break ;
  149.                                              // fall through
  150.      case WM_LBUTTONUP :
  151.           SendMessage (GetParent (hwnd), WM_COMMAND,
  152.                GetWindowLong (hwnd, GWL_ID), (LPARAM) hwnd) ;
  153.           return 0 ;
  154.      }
  155.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  156. }
  157.